﻿namespace MSSqlSchemaDoc.Core.DataStructures
{
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Serialization;

    /// <summary>
    /// Provides a base implementation for Dml and Ddl triggers.
    /// </summary>
    public abstract class TriggerBase : DataStructureObjectBase
    {
        /// <summary>
        /// Gets or sets the object_id of the table which is the trigger's parent.
        /// If the id is 0, then the database is the owner of the trigger (and it is a DDL trigger).
        /// </summary>
        [XmlIgnore()]
        public int ParentId { get; set; }

        /// <summary>
        /// Gets or sets the events that cause a trigger to fire.
        /// </summary>
        [XmlArray()]
        public Collection<TriggerEvent> Events { get; set; }

        /// <summary>
        /// Gets or sets a string determining whether the trigger applies to DDL or DML operations.
        /// </summary>
        [XmlElement()]
        public string OperationType { get; set; }

        /// <summary>
        /// Gets or sets a string determining whether the trigger is implemented in T-SQL or in the .Net CLR.
        /// </summary>
        [XmlElement()]
        public string ImplementationType { get; set; }

        /// <summary>
        /// Gets or sets the T-SQL which defines the trigger (the body of the trigger).
        /// </summary>
        [XmlIgnore()]
        public string Definition { get; set; }

        /// <summary>
        /// Gets or sets the definition wrapped as a "CData" section.
        /// Only used for serialization.
        /// </summary>
        [XmlElement("Definition")]
        public XmlCDataSection XmlCDataDefinition
        {
            get
            {
                XmlDocument d = new XmlDocument();
                return d.CreateCDataSection(this.Definition);
            }

            set
            {
                this.Definition = value.CreateNavigator().InnerXml;
            }
        }
    }
}
